<!DOCTYPE html>
<html lang="en">

<head>
  <!-- head and css -->
  {% include 't_head.html' %}
</head>

<body>
  <div class="d-flex" id="wrapper">
    <!-- Sidebar -->
    {% include 't_sidebar.html' %}

    <!-- Page Content -->
    <div id="page-content-wrapper">

      <!-- Navbar -->
      {% include 't_navbar.html' %}

      <!-- 2 textarea side by side -->
      <div class="container-fluid">
        <br>
        <h2>File Manager 🗂 - [BETA - use ADB/SCP to download files]</h2>
        <div class="border-top my-3"></div>
        <h4><font color="ff5050">{{target_package}}</font> - enviroment info:</h4>
        <ul>
        <!-- Android start -->
        {% if mobile_OS=="Android" %}
        <li>
          <b>mainDirectory:</b> 
          <a href="/file_manager?path={{env.mainDirectory}}">{{env.mainDirectory}}</a>
        </li>
        <li>
          <b>filesDirectory:</b> 
          <a href="/file_manager?path={{env.filesDirectory}}">{{env.filesDirectory}}</a>
        </li>
        <li>
          <b>cacheDirectory:</b> 
          <a href="/file_manager?path={{env.cacheDirectory}}">{{env.cacheDirectory}}</a>
        </li>
        <li>
          <b>externalCacheDirectory:</b> 
          <a href="/file_manager?path={{env.externalCacheDirectory}}">{{env.externalCacheDirectory}}</a>
        </li>
        <li>
          <b>codeCacheDirectory:</b> 
          <a href="/file_manager?path={{env.codeCacheDirectory}}">{{env.codeCacheDirectory}}</a>
        </li>
        <li>
          <b>obbDir:</b> 
          <a href="/file_manager?path={{env.obbDir}}">{{env.obbDir}}</a></li>
        <li>
          <b>packageCodePath:</b> 
          <a href="/file_manager?path={{env.packageCodePath}}">{{env.packageCodePath}}</a>
        </li>
        {% endif %}
        <!-- Android end -->
        <!-- iOS start -->
        {% if mobile_OS=="iOS" %}
        <li>
          <b>mainDirectory:</b> 
          <a href="/file_manager?path={{env.mainDirectory}}">{{env.mainDirectory}}</a>
        </li>
        <li>
          <b>BundlePath:</b> 
          <a href="/file_manager?path={{env.BundlePath}}">{{env.BundlePath}}</a>
        </li>
        <li>
          <b>CachesDirectory:</b> 
          <a href="/file_manager?path={{env.CachesDirectory}}">{{env.CachesDirectory}}</a>
        </li>
        <li>
          <b>DocumentDirectory:</b> 
          <a href="/file_manager?path={{env.DocumentDirectory}}">{{env.DocumentDirectory}}</a>
        </li>
        <li>
          <b>LibraryDirectory:</b> 
          <a href="/file_manager?path={{env.LibraryDirectory}}">{{env.LibraryDirectory}}</a>
        </li>
        {% endif %}
        <!-- iOS end -->

        </ul>
        <div class="border-top my-3"></div>
          {% if currentPath %}
          <h5>&nbsp;&nbsp;<font color="3399ff">Current Path:</font> {{currentPath}}</h5>
          <br>
          <table class="table w-auto">
            <thead>
              <th class="text-center">icon</th>
              <th>filename</th>
              <th class="text-center">isHidden</th>
              <th class="text-center">size</th>
              <th class="text-center">lastModified</th>
              {% if mobile_OS == "Android" %}
              <th class="text-center">adb command</th>
              {% else %}
              <th class="text-center">file path</th>
              {% endif %}
              {% if BETA == true %}
              <th class="text-center">download</th>
              {% endif %}
            </thead>
            <tbody>
              {% set pathlength = ((files_at_path.path).split("/")) | length %}
              {% set parent = ((files_at_path.path).split("/")).slice(0,pathlength-1).join("/")  %}
              <!-- Android start -->
              {% if 
                mobile_OS == "Android" and
                files_at_path.path != env.mainDirectory and 
                files_at_path.path != env.externalCacheDirectory and 
                files_at_path.path != env.obbDir 
              %}
            <tr>
              <td><a href="/file_manager?path={{parent}}">..</a></td>
            </tr>
            {% endif %}
            <!-- Android end -->
            <!-- iOS start TODO -->
            {% if 
              mobile_OS == "iOS" and
              files_at_path.path != env.mainDirectory and
              files_at_path.path != env.BundlePath 
            %}
          <tr>
            <td><a href="/file_manager?path={{parent}}">..</a></td>
          </tr>
          {% endif %}
          <!-- iOS end -->
            {% for f, item in files_at_path["files"] %}
            <tr>
              <td class="text-center">
                {% if files_at_path.files[f].attributes.isDirectory %}
                📁
                {% else %}
                🗳
                {% endif %}
              </td>
                <td>
                  {% if files_at_path.files[f].attributes.isDirectory %}
                  <a href="/file_manager?path={{files_at_path.path}}/{{f}}">{{f}}</a>
                  {% else %}
                    {{f}}
                  {% endif %}
                </td>
                <td class="text-center">
                  {% if files_at_path.files[f].attributes.isHidden == True %}
                  ❗️
                  {% else %}
                  -
                  {% endif %}
                </td>
                <td class="text-center">
                  {{files_at_path.files[f].attributes.size}}
                </td>
                <td class="text-center">
                  {{files_at_path.files[f].attributes.lastModified}}
                </td>
                <td class="text-center">
                  {% if files_at_path.files[f].attributes.isFile %}
                  <button 
                  onclick="show_adb_command('{{files_at_path.path}}','{{f}}','{{mobile_OS}}')"
                  class="btn btn-primary btn-sm">
                  show
                  </button>
                  <button 
                  onclick="copy_adb_command('{{files_at_path.path}}','{{f}}','{{mobile_OS}}')"
                  class="btn btn-warning btn-sm">
                  copy
                  </button>
                  {% else %}
                  -
                  {% endif %}
                </td>
                {% if BETA == true %}
                <td class="text-center">
                  {% if files_at_path.files[f].attributes.isFile %}
                  <a
                  role="button"
                  class="btn btn-danger btn-sm"
                  href="/file_manager?download={{files_at_path.path}}/{{f}}">Download
                </a>
                  {% else %}
                  -
                  {% endif %}
                </td>
                {% endif %}
            </tr>
            </tbody>
            {% endfor %}
            </table>
          {% endif %}
    </div>
    <!-- /#container-fluid -->

  </div>
  <!-- /#page-content-wrapper -->

  </div>
  <!-- /#wrapper -->

  <!-- Javascript loading -->
  {% include 't_js_script.html' %}

  {% block scripts %}
  <script>
    function show_adb_command(path,filename,os){ 
      if(os=="Android"){
        if (filename=="base.apk")
          alert("adb pull "+path+"/"+filename+ " {{target_package}}.apk")
        else alert("adb pull "+path+"/"+filename)
      }
      else{
        alert(path+"/"+filename)
      }

    }

    function copy_adb_command(path,filename,os){ 
      var textToCopy=""
      if(os=="Android"){
        if (filename=="base.apk")
          textToCopy="adb pull "+path+"/"+filename+" {{target_package}}.apk"
        else textToCopy="adb pull "+path+"/"+filename
      }
      else{
        textToCopy=path+"/"+filename
      }

      var inp =document.createElement('input');
      document.body.appendChild(inp)
      inp.value =textToCopy
      inp.select();
      document.execCommand('copy',false);
      inp.remove();
    }
  </script>

  <!-- Set current Page as active in the NavBar -->
  <script>
    $(document).ready(function () {
      $(".nav li").removeClass("active");
      $('#file_manager').addClass('active');
    });
    </script>
  <!-- Set current Page as active in the NavBar -->

  {% endblock %}

</body>

</html>